package controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.AdminMessage;
import pojo.BookBorrow;
import pojo.BookMessage;
import pojo.UserMessage;
import service.AdminBookService;
import service.UserBookService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author 小陈
 */
@Controller
@RequestMapping("/admin")
public class adminAction {
    /**
     * 自动装配,给service层变量赋值
     */
    @Autowired
    private AdminBookService adminBookService;
    @Autowired
    private UserBookService userBookService;

    /**
     * 到达管理员总功能页面
     */
    @RequestMapping("/toadminAction")
    public String touserAction() {
        return "adminAction";
    }

    /**
     * 管理员获取所有图书
     */
    @RequestMapping("/admingetBooks")
    public String admingetBooks(Model model, @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = false, defaultValue = "5") int size) {
        PageHelper.startPage(page, size);//使用PageHelper分页
        List<BookMessage> book = adminBookService.getAllBooks();//调用service层方法获取所有书籍信息
        PageInfo<BookMessage> pageInfo = new PageInfo<>(book);//将书籍信息注入到PageInfo分页bean中
        List<BookMessage> books = pageInfo.getList();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("books", books);
        return "adminBookList";
    }

    /**
     * 管理员搜索某一图书
     */
    @RequestMapping("/searchBook")
    public String searchBook(Model model, String bookName, @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                             @RequestParam(name = "size", required = false, defaultValue = "5") int size) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("bookInfo", bookName.trim());
        PageHelper.startPage(page, size);
        List<BookMessage> book = adminBookService.getBooksByInfo(map);
        PageInfo<BookMessage> pageInfo = new PageInfo<>(book);
        List<BookMessage> books = pageInfo.getList();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("books", books);
        model.addAttribute("bookName", bookName);
        System.out.println(books);
        System.out.println(pageInfo.getTotal());

        return "SearchBookList";
    }

    /**
     * 管理员删除某一图书
     */
    @RequestMapping("/delete/{bookName}")
    public String delete(@PathVariable String bookName) {
        adminBookService.deleteBook(bookName);
        return "redirect:/admin/admingetBooks";
    }

    /**
     * 管理员添加图书，使页面跳转到输入图书信息页面
     */
    @RequestMapping("/toaddBook")
    public String toAddBook() {
        return "addBook";
    }

    /**
     * 获取图书信息后，调用service层方法，将书籍插入到数据表中
     */
    @RequestMapping("/addBook")
    @ResponseBody
    public String addBook(BookMessage book) {
        String str = "";
        int d = adminBookService.addBook(book);
        if (d > 0) {
            str += "添加成功";
        } else {
            str += "添加失败";
        }
        return str;
    }

    /**
     * 管理员获取所有的借阅信息
     */
    @RequestMapping("/getAllLender")
    public String getAllLender(Model model) {
        List<BookBorrow> list = adminBookService.getAlllender();
        model.addAttribute("list", list);
        return "AllLender";
    }

    /**
     * 管理员搜索某一用户的借阅信息
     */
    @RequestMapping("/searchLender")
    public String searchLender(Model model, String readerName) {
        List<BookBorrow> list = adminBookService.getAllLender(readerName.trim());
        model.addAttribute("list", list);
        return "AllLender";
    }

    /**
     * 管理员获取个人信息
     */
    @RequestMapping("/toupdate")
    public String toUpdate(Model model, HttpServletRequest req, @RequestParam int action) {
        int a = 2;
        String adminName = (String) req.getSession().getAttribute("adminName");
        AdminMessage admin = adminBookService.getAdmin(adminName);
        model.addAttribute("admin", admin);
        if (action == a) {
            return "adminMessage";
        } else {
            return "UpdateAdminMessage";
        }
    }

    /**
     * 管理员修改个人信息
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(AdminMessage admin) throws IOException {
        int d = adminBookService.UpdateAdminMesssage(admin);
        String str = "";
        if (d > 0) {
            str += "修改成功";
        } else {
            str += "修改失败";
        }
        return str;
    }

    /**
     * 管理员获取所有用户信息
     */
    @RequestMapping("/touser")
    public String touser(Model model) {
        List<UserMessage> users = adminBookService.getAlluser();
        model.addAttribute("users", users);
        return "AllUserMessage";
    }

    /**
     * 管理员获取某一用户信息
     */
    @RequestMapping("/searchUser")
    public String searchUser(Model model, String userName) {
        List<UserMessage> users = adminBookService.getuser(userName.trim());
        model.addAttribute("users", users);
        return "AllUserMessage";
    }

    /**
     * 管理员获取某一书籍信息
     */
    @RequestMapping("/toUpdate/{bookName}")
    public String toUpdate(@PathVariable String bookName, Model model) {
        BookMessage book = adminBookService.getBook(bookName.trim());
        model.addAttribute("book", book);
        return "updateBookMessage";
    }

    /**
     * 管理员修改某一书籍信息
     */
    @RequestMapping("/updataBook")
    @ResponseBody
    public String update(BookMessage book) throws IOException {
        String str = "";
        int i = adminBookService.UpdateBookMessage(book);
        if (i > 0) {
            str += "修改成功";
        } else {
            str += "修改失败";
        }
        return str;
    }

    /**
     * 管理员删除某一用户
     */
    @RequestMapping("/deleteUser/{userName}")
    public String deleteUser(@PathVariable String userName, Model model) {
        int i = adminBookService.getLends(userName);
        if (i == 0) {
            userBookService.deleteUserByname(userName);
            model.addAttribute("str", null);
        } else {
            model.addAttribute("str", "存在借阅记录无法删除!");
        }
        List<UserMessage> users = adminBookService.getAlluser();
        model.addAttribute("users", users);
        return "AllUserMessage";
    }

    /**
     * 管理员获取某一用户信息
     */
    @RequestMapping("updateUser/{userName}")
    public String updateUser(@PathVariable String userName, Model model) {
        UserMessage user = userBookService.getUserMessage(userName);
        model.addAttribute("user", user);
        return "AdminUpdateUserMessage";
    }

    /**
     * 管理员修改某一用户信息
     */
    @RequestMapping("/updateUserMessage")
    @ResponseBody
    public String updateUserMessage(UserMessage user) {
        int i = userBookService.UpdateuserMessage(user);
        String str = "";
        if (i > 0) {
            str += "修改成功";
        } else {
            str += "修改失败";
        }
        return str;
    }
}